﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class certification_employee : System.Web.UI.Page
{
    protected DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log.aspx", true);

        }
        if (Session["IDENTITY"].Equals("employee") == true)
        {
            Response.Redirect("error_auth.aspx", true);
        }
        else
        {
            dt.Columns.Add("name");
            dt.Columns.Add("address");
            dt.Columns.Add("phone");
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand get_uncert = new SqlCommand();
            get_uncert.Connection = sc;
            get_uncert.CommandText = "select EMPLOYEE_NAME,EMPLOYEE_ADDRESS,EMPLOYEE_PHONE from INFO_EMPLOYEE where CERTIFICATION=0 and ENTER_ID=@eid1";
            get_uncert.Parameters.AddWithValue("@name",Session["USERNAME"].ToString());
            get_uncert.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
            SqlDataReader r = get_uncert.ExecuteReader();
            while (r.Read())
            {
                DataRow row = dt.NewRow();
                row["name"] = r.GetString(r.GetOrdinal("EMPLOYEE_NAME"));
                row["address"] = r.GetString(r.GetOrdinal("EMPLOYEE_ADDRESS"));
                row["phone"] = r.GetString(r.GetOrdinal("EMPLOYEE_PHONE"));
                dt.Rows.Add(row);
            }
            r.Close();
            sc.Close();
            SqlConnection sc2 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc2.Open();
            SqlCommand get_count = new SqlCommand("select count(EMPLOYEE_NAME) from INFO_EMPLOYEE where ENTER_ID=@eid1 and CERTIFICATION=0", sc2);
            get_count.Parameters.AddWithValue("@name", Session["USERNAME"].ToString());
            get_count.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
            int uncertifiedcount = Convert.ToInt32(get_count.ExecuteScalar().ToString());
            sc2.Close();
            if (uncertifiedcount == 0)
                la2.InnerText = "您没有需要审核的信息。";
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Approve")
        {
            string name = e.CommandArgument.ToString();
            try
            {
                SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
                sc.Open();
                SqlCommand update_cert = new SqlCommand("update INFO_EMPLOYEE set CERTIFICATION=1 where EMPLOYEE_NAME=@ename", sc);
                update_cert.Parameters.AddWithValue("@ename", name);

                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                    update_cert.ExecuteNonQuery();
                    Application["sqllock"] = "unlock";//解锁 
                sc.Close();
            }
            catch (Exception ex_sql)
            {
                Application["sqllock"] = "unlock";//解锁
                Page.RegisterStartupScript("ss", "<script>alert('"+ex_sql.Message+ex_sql.StackTrace+"!')</script>");
                return;
            }

        }
        else if (e.CommandName == "Reject")
        {
            string name = e.CommandArgument.ToString();
            try
            {
                SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
                sc.Open();
                SqlCommand dele_cert = new SqlCommand("delete from INFO_EMPLOYEE where EMPLOYEE_NAME=@ename1", sc);
                dele_cert.Parameters.AddWithValue("@ename1",name);
                while (Application["sqllock"] == "lock")
                {//检测是否已被锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁          
                dele_cert.ExecuteNonQuery();
                Application["sqllock"] = "unlock";//解锁
                
                sc.Close();
            }
            catch (Exception ex_sql)
            {
                Application["sqllock"] = "unlock";//解锁
                Page.RegisterStartupScript("ss", "<script>alert('" + ex_sql.Message + ex_sql.StackTrace + "!')</script>");
                return;
            }
        }
    }
}